﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using Web.PuntoDeVenta.Code.Contracts;
using Web.PuntoDeVenta.Code;
using Web.Framework;
using System.Web.Security;
using System.Web;
using CrmContacto.Contracts;
using Web.Framework.Translate;
using CrmContacto.Contracts.Contracts.Search;
using Web.Framework.Svc;
using Web.Framework.Validation;

namespace Web.PuntoDeVenta.Svc
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ClientSearch
    {
        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
        public List<CrmContacto.Contracts.Contracts.PuntoDeVenta> GetPuntosDeVenta(int empresaId)
        {
            List<CrmContacto.Contracts.Contracts.PuntoDeVenta> returnList = new List<CrmContacto.Contracts.Contracts.PuntoDeVenta>();
            
            if (empresaId != -1)
            {
                PuntoDeVentaSearch search = new PuntoDeVentaSearch();
                search.EmpresaId = empresaId;
                search.Activo = true;
                search.LoadSubPuntos = true;
                returnList.AddRange(WebServiceHelper.GetPuntoDeVentaDto(new ServiceOptions(), search));
            }
            else
            {
                returnList.Add(new CrmContacto.Contracts.Contracts.PuntoDeVenta() { Id = Convert.ToInt32(UIHelper.cboNullValue), Nombre = string.Empty });
            }

            return returnList;
        }

        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
        public List<MaestroBase> GetProvincias(int paisId)
        {
            List<MaestroBase> returnList = new List<MaestroBase>();
            string sinProvincia = LocalizationProvider.Instance["CboSinProvincia"];
            returnList.Add(new MaestroBase() { Id = Convert.ToInt32(UIHelper.cboNullValue), Nombre = sinProvincia });
            if (paisId != -1)
            {
                returnList.AddRange(WebServiceHelper.GetProvincias(paisId));                
            }          

            return returnList;
        }

        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
        public bool ValidarEmail(string emailaddress)
        {
            RegexUtility u = new RegexUtility();
            return u.IsValidEmail(emailaddress);
        }
    }
}
